<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>分糖果</title>
</head>

<body>
  <script>
    var candy = function (ratings) {
      let i = 0, r = new Uint16Array(ratings.length).fill(1), j = ratings.length - 1
      while (i < ratings.length - 1) {
        if (ratings[++i] > ratings[i - 1]) r[i] = Math.max(r[i], r[i - 1] + 1)
        if (ratings[--j] > ratings[j + 1]) r[j] = Math.max(r[j], r[j + 1] + 1)
      }
      return r.reduce((p, v) => p + v)
    };
    console.log(candy([1, 0, 1]))
  </script>

</body>

</html>